package cz.muni;

import cz.muni.restinterface.CourierDTO;
import cz.muni.service.CourierServiceLocal;
import java.io.IOException;
import java.io.PrintWriter;
import javax.ejb.EJB;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

@WebServlet("/processLogin")
public class ProcessLoginServlet extends HttpServlet {

    private static final long serialVersionUID = 1L;
    @EJB(mappedName = "java:global/DeliveryCompany-web/CourierService!cz.muni.service.CourierServiceLocal")
    private CourierServiceLocal courierService;

    public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        response.setContentType("text/html; charset=UTF-8");
        response.setCharacterEncoding("UTF-8");

        String name = (String) request.getAttribute("name");
        String pass = (String) request.getAttribute("password");
        CourierDTO courier;
        ServletContext context = getServletContext();
        PrintWriter out = response.getWriter();

        try {
            if (name.equals("admin") && pass.equals("admin")) {
                RequestDispatcher rd = context.getRequestDispatcher("/managerHome.jsp");
                HttpSession session = request.getSession(true);
                session.setAttribute("loggedIn", true);
                session.setAttribute("role", "admin");
                rd.forward(request, response);
            } else if ((courier = courierService.find(Long.valueOf(name))) != null) {
                if (courier.getUserName().equals(pass)) {
                    RequestDispatcher rd = context.getRequestDispatcher("/courier_shipments.jsp");
                    HttpSession session = request.getSession(true);
                    session.setAttribute("loggedIn", true);
                    session.setAttribute("role", "courier");
                    rd.forward(request, response);
                } else {
                    out.println("Invalid courier " + courier.getId().toString() + "param pass:" + pass + "courier pass:" + courier.getUserName());
                    RequestDispatcher rd = context.getRequestDispatcher("/LoginFailed.jsp");
                    rd.forward(request, response);
                }
            } else {
                out.println("Invalid user" + courier.getId().toString());
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            RequestDispatcher rd = context.getRequestDispatcher("/LoginFailed.jsp");
            rd.forward(request, response);
        }
    }

    public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request, response);
    }
}
